package com.itboye.sunsun.custome;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannedString;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;

import com.itboye.homesensen.alert.ByAlert;
import com.itboye.sunsun.application.App;
import com.itboye.sunsun.utils.Spanned2String;
import com.itboye.sunsunhome.R;

public class PictureAndTextEditorView extends EditText {  
    private final String TAG = "PATEditorView";  
    private Context mContext;  
    private List<String> mContentList;  
  
    public static final String mBitmapTag = "☆";  
    private String mNewLineTag = "\n";  
  
    public PictureAndTextEditorView(Context context) {  
        super(context);  
        System.out.println("--------------one");
        init(context);  
        
    }  
  
    public PictureAndTextEditorView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        init(context);  

        System.out.println("--------------two");
    }  
  
    public PictureAndTextEditorView(Context context, AttributeSet attrs, int defStyleAttr) {  
        super(context, attrs, defStyleAttr);  
        init(context);  

        System.out.println("--------------three");
    }  
  
    private void init(Context context) {  
        mContext = context;  
        mContentList = getmContentList();  
        insertData();  
    }  
  
    /** 
     * 设置数据 
     */  
    private void insertData() {  
        if (mContentList.size() > 0) {  
            for (String str : mContentList) {  
                if (str.indexOf(mBitmapTag) != -1) {//判断是否是图片地址  
                    String path = str.replace(mBitmapTag, "");//还原地址字符串  
                    Bitmap bitmap = getSmallBitmap(path, 480, 200);  
                    if(path.length()>1){
                    }
                    //插入图片  
                    insertBitmapPath(path, bitmap);  
                } else {  
                    //插入文字  
                    SpannableString ss = new SpannableString(str);  
                    append(ss);  
                }  
            }  
        }  
    }  
  
    /** 
     * 插入图片 
     * 
     * @param bitmap 
     * @param path 
     * @return 
     */  
    private SpannableString insertBitmapPath(String path, Bitmap bitmap) {  
        Editable edit_text = getEditableText();  
        int index = getSelectionStart(); // 获取光标所在位置  
        //插入换行符，使图片单独占一行  
        SpannableString newLine = new SpannableString("\n");  
        edit_text.insert(index, newLine);//插入图片前换行  
        // 创建一个SpannableString对象，以便插入用ImageSpan对象封装的图像  
        path = mBitmapTag + path + mBitmapTag;  
        SpannableString spannableString = new SpannableString(path);  
        // 根据Bitmap对象创建ImageSpan对象  没有下面两句话返回的不是图片，是一个图片名称
        
        
        ImageSpan imageSpan = new ImageSpan(mContext, bitmap); // SPAN_EXCLUSIVE_EXCLUSIVE
        // 用ImageSpan对象替换你指定的字符串  
        spannableString.setSpan(imageSpan, 0, path.length(), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);  
        // 将选择的图片追加到EditText中光标所在位置  
        if (index < 0 || index >= edit_text.length()) {  
        	System.out.println("执行一次-------------------------");
            edit_text.append(spannableString);  
        } else {  
        	System.out.println("执行二次-------------------------");
            edit_text.insert(index, spannableString);  
//            edit_text.
        }  
        edit_text.insert(index, newLine);//插入图片后换行
        return spannableString;  
    }  
  
  
    /** 
     * 插入图片 添加图片调用
     * 
     * @param path 
     */  
    public void insertBitmap(String path) { 
    	
    	System.out.println(">>>>>>>>>>>>>>>>>"+path);
        Bitmap bitmap = getSmallBitmap(path, 480, 200);  
        if(path.length()>1){
        	Log.i("LLLLLLLLLLL", path.length()+"");
        	
        }
        insertBitmapPath(path, bitmap);  
    }  
  
    /** 
     * 用集合的形式获取控件里的内容 
     * 
     * @return 
     */  
    public List<String> getmContentList() {  
        if (mContentList == null) {  
            mContentList = new ArrayList<String>();  
        }  
        System.out.println("编辑框内容"+getText().toString());
        String content=Spanned2String.parse(getText());
        content= content.replaceAll(mNewLineTag, "");  
        if (content.length() > 0 && content.contains(mBitmapTag)) {
            String[] split = content.split("☆");  
            mContentList.clear();  
            for (String str : split) {  
                mContentList.add(str);  
            }  
        } else {  
            mContentList.add(content);  
        }  
  
        return mContentList;  
    }  
  
    /** 
     * 设置显示的内容集合 
     * 
     * @param contentList 
     */  
    public void setmContentList(List<String> contentList) {  
        if (mContentList == null) {  
            mContentList = new ArrayList<String>();  
        }  
        this.mContentList.clear();  
        this.mContentList.addAll(contentList);  
        insertData();  
    }  
  
    float oldY = 0;  
  
    @Override  
    public boolean onTouchEvent(MotionEvent event) {  
  
        switch (event.getAction()) {  
            case MotionEvent.ACTION_DOWN:  
                oldY = event.getY();  
                requestFocus();  
                break;  
            case MotionEvent.ACTION_MOVE:  
                float newY = event.getY();  
                if (Math.abs(oldY - newY) > 20) {  
                    clearFocus();  
                }  
                break;  
            case MotionEvent.ACTION_UP:  
                break;  
            default:  
                break;  
        }  
        return super.onTouchEvent(event);  
    }  
  
    // 根据路径获得图片并压缩，返回bitmap用于显示  
    public Bitmap getSmallBitmap(String filePath, int reqWidth, int reqHeight) {  
        final BitmapFactory.Options options = new BitmapFactory.Options();  
        options.inJustDecodeBounds = true;  
//        BitmapFactory.decodeFile(filePath, options);  
  
        // Calculate inSampleSize  
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);  
  
        // Decode bitmap with inSampleSize set  
        options.inJustDecodeBounds = false;  
  
        Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);  
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();  
        int w_screen = dm.widthPixels;  
        int w_width = w_screen;  
        int b_width = bitmap.getWidth();  
        int b_height = bitmap.getHeight();  
        int w_height = w_width * b_height / b_width;  
        bitmap = Bitmap.createScaledBitmap(bitmap, w_width, w_height, false);  
        return bitmap;  
    }  
  
    //计算图片的缩放值  
    public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {  
        final int height = options.outHeight/2;  
        final int width = options.outWidth;  
        int inSampleSize = 1;  
  
        if (height > reqHeight || width > reqWidth) {  
            final int heightRatio = Math.round((float) height / (float) reqHeight);  
            final int widthRatio = Math.round((float) width / (float) reqWidth);  
            inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;  
        }  
        return inSampleSize;  
    } 
   
}
